package com.satan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.satan.entity.SysUserRole;
import com.satan.mapper.SysUserRoleMapper;
import com.satan.service.SysUserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author Demon
* @description 针对表【sys_user_role】的数据库操作Service实现
* @createDate 2023-10-03 20:53:34
*/
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole>
    implements SysUserRoleService{

    @Override
    @Transactional
    public boolean grantRole(Long userId, Long[] roleIds) {
        List<SysUserRole> userRoleList=new ArrayList<>();
        Arrays.stream(roleIds).forEach(r -> {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setRoleId(r);
            sysUserRole.setUserId(userId);
            userRoleList.add(sysUserRole);
        });
        boolean b1 = remove(new QueryWrapper<SysUserRole>().eq("user_id", userId));
        boolean b2 = saveBatch(userRoleList);
        return b1&&b2;
    }
}




